<?php
/**
 * Created by PhpStorm.
 * User: 76683
 * Date: 2018/7/17
 * Time: 21:22
 */

namespace app\api\controller;

use app\api\model\WeCategory as WeCategoryModel;
use app\api\model\WeTopic;
use app\api\model\WeDayTopic;
use think\Request;
use app\api\model\WeAds;
use app\api\model\WeTopicDetail;

class WeCategory extends Base
{

    protected $weDayTopic = null;

    protected $weAds;

    protected $weTopicDetail = null;

    function __construct(Request $request = null)
    {
        $this->weDayTopic = new WeDayTopic();
        $this->weAds = new WeAds();
        $this->weTopicDetail = new WeTopicDetail();
        parent::__construct($request);
    }

    /**
     *    * 热门领域
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index(){
        $model = new WeCategoryModel();
        $data_list = $model->hot();
        $user_focus = \app\api\model\WeUserCategory::userCateAll($this->uid);
        $data_list['data'] = array_map(function ($item) use ($user_focus){
            if (in_array($item['id'],$user_focus)){
                $item['is_focus'] = 1;
            }else{
                $item['is_focus'] = 0;
            }
            return $item;
        },$data_list['data']);
        $ads = $this->weAds->dayAds();
        $data_list['ads'] = $ads;
        list($this->code,$this->msg,$this->data) = [1,'热门领域请求成功',$data_list];
        return;
    }

    /**
     * 领域搜索接口
     * @throws \think\exception\DbException
     */
    public function search(){
        $keyWords = input('keywords','','','strval');
        $model = new WeCategoryModel();
        $data_list = $model->search($keyWords);
        $user_focus = \app\api\model\WeUserCategory::userCateAll($this->uid);
        $data_list['data'] = array_map(function ($item) use ($user_focus){
            if (in_array($item['id'],$user_focus)){
                $item['is_focus'] = 1;
            }else{
                $item['is_focus'] = 0;
            }
            return $item;
        },$data_list['data']);
        list($this->code,$this->msg,$this->data) = [1,'领域搜索成功',$data_list];
        return;
    }

    /**
     * 领域申请
     */
    public function applyCate(){
        $uid = $this->uid;
        if(!$uid){
            list($this->code,$this->msg) = [0,'请登陆'];
        }
        $cate = $this->request->post();
        $res = $this->validate($cate,'Category.apply');
        if(true !== $res){
            list($this->code,$this->msg) = [0,$res];
            return;
        }
        $model = new WeCategoryModel();
        $result =$model->applyCate($cate);
        if($result){
            list($this->code,$this->msg) = [1,'申请成功，请等待管理员审核'];
            return;
        }
    }

    /**
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function topicList(){
        $data = $this->request->post();
        $res = $this->validate($data,'Category.list');
        if(true !== $res){
            list($this->code,$this->msg) = [0,$res];
            return;
        }
        $topic = new WeTopic();
        $uid = $this->uid;
        $data_list = $topic->topicList($data,$uid);
        $ads = $this->weAds->dayAds();
        $today = $todayTopic = $this->weDayTopic->todayTopic($this->uid);
        list($this->code,$this->msg,$this->data) = [1,'领域话题列表请求成功',['day'=>$today,'ads' => $ads,'topic_list'=>$data_list]];
        return;
    }


    public function cateList(){
        $data_list = \app\api\model\WeCategory::where(['status'=>1,'is_delete'=>0,'is_verify'=>1])->field('id,name')->paginate(config('sd_config.list_len'))->toArray();
        list($this->code,$this->msg,$this->data) = [1,'领域列表请求成功',$data_list];
        return;
    }


    public function theReply(){
        $category_id = $this->request->post('category_id');

        //获取最新回复列表 时间倒序
        $ids = $this->weTopicDetail->replyDesc();
        if (empty($ids) || !is_array($ids)){
            list($this->code,$this->msg,$this->data) = [1,'列表获取成功',[]];
            return;
        }
        $map = ['t.id'=>['in',$ids]];
        $topic = new WeTopic();
        $data_list = $topic->topicList(array_merge($map,['t.category_id'=>$category_id]));
        list($this->code,$this->msg,$this->data) = [1,'列表获取成功',$data_list];
        return;
    }
}